package com.zxn.ssyx.controller;

import com.zxn.ssyx.common.result.Result;
import com.zxn.ssyx.model.acl.Permission;
import com.zxn.ssyx.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/acl/permission")
@Api(tags = "菜单管理")
@CrossOrigin  // 跨域
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "获取菜单")
    @GetMapping
    public Result index(){
        List<Permission> permissionList = permissionService.getWithTree();
        return Result.ok(permissionList);
    }

    @ApiOperation(value = "新增菜单")
    @PostMapping("/save")
    public Result save(@RequestBody Permission permission){
        boolean result = permissionService.save(permission);
        if (result){
            return Result.ok("保存菜单成功！！");
        }
        return Result.fail("保存菜单失败！！");
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("/update")
    public Result updateById(@RequestBody Permission permission) {
        boolean result = permissionService.updateById(permission);
        if (result){
            return Result.ok("修改菜单成功！！");
        }
        return Result.fail("修改菜单失败！！");
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/remove/{id}")
    public Result removeById(@PathVariable Long id){
        boolean result = permissionService.removeChildById(id);
        if (result){
            return Result.ok("删除菜单成功！！");
        }
        return Result.fail("删除菜单失败！！");
    }
}
